package cate.game.play.proce.round;

import cate.game.play.config.AllPlayCfg;
import cate.game.play.fighter.Fighter;
import cate.game.play.part.TeamVS;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.BackActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.play.PlayCtx;
import cate.game.play.proce.turn.AppendTurn;
import cate.game.play.proce.turn.TurnCtx;
import cate.game.play.skill.Skill;
import cate.game.play.skill.SkillSelector;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

@NoteClass(value = "轮次ctx",detail = "")
public class RoundCtx {

	@NoteField("本回合的战斗记录数据")
	public RoundPO po = new RoundPO();

	@NoteField("剧本ctx")
	private PlayCtx play;

	@NoteField("第几回合")
	public int index;

	@NoteField("追加行动轮次")
	private LinkedList<AppendTurn> appendTurns = new LinkedList<>();

	@NoteField("buff处理器")
	private RoundBuffHandler buff = new RoundBuffHandler(this);

	@NoteField("圣器处理器")
	private RoundHallowHandler hallow = new RoundHallowHandler(this);

	@NoteField("灵宠处理器")
	private RoundPetHandler pet = new RoundPetHandler(this);

	@NoteField("统计")
	public RoundStat stat = new RoundStat();

	public RoundCtx(PlayCtx play, int index) {
		this.play = play;
		this.index = index;
	}

	//回合开始时的处理顺序
	// 1、英雄登场处理(添加被动buff,处理开场释放技能,被动技能开场处理)
	// 2、被动技能回合开始处理
	// 3、buff回合开始处理
	// 4、圣器技能释放
	// 5、灵宠技能释放
	// 6、英雄释放开场技能

	public void begin() {
		this.play.addRound(this);
		this.play.teamVS.onRoundBegin(this);
		this.buff.onBegin();
		this.hallow.onBegin();
		this.pet.onBegin();
		//追加轮次处理
		handleAppendTurn();
	}


	public void ing() {
		LinkedList<Fighter> afs = new LinkedList(play.teamVS.getAllAreaFighters());
		Collections.shuffle(afs);
		for (Fighter f : afs) {
			Skill s = SkillSelector.selectOneByCd(this, f);
			if (s != null) {
				if (s.active != null) {
					s.active.onPreSelectByCD(this);
				}
				f.skill.forHandlers(h->h.onPreSelectByCD(this, s));
			}
		}
		while (!afs.isEmpty()) {
			if (play.teamVS.isOver()) {
				break;
			}
			afs.sort(Comparator.comparing((Fighter f) -> !f.status.isIgnSpeedSort()).
					thenComparing(f -> -f.attr.total().speedTotal()));
			Fighter f = afs.removeFirst();
			TurnCtx turnCtx = new TurnCtx(this, f, f.status.getTurnSkill(), false);
			//开始行动
			turnCtx.start();
			//追加轮次处理
			handleAppendTurn();
		}
	}


	public void end() {
		//回合结束前的处理
		this.buff.onEnd();
		this.play.teamVS.onRoundEnd(this);
		//追加轮次处理()
		handleAppendTurn();
	}

	/** 添加一个action */
	public void addAction(ActionCtx action) {
		this.po.addAction(action.getPo());
	}

	//构建时间行为
	public TimeActionCtx buildTimeAction(){
		TimeActionCtx action = new TimeActionCtx(this);
		this.addAction(action);
		return action;
	}

	//构建归为行为
	public BackActionCtx buildBackAction(int actorPid){
		BackActionCtx action = new BackActionCtx(this,actorPid);
		this.addAction(action);
		return action;
	}

	/**
	 * 追加行动轮次
	 * @param turn
	 */
	public void addAppendTurn(AppendTurn turn){
		appendTurns.add(turn);
	}


	/**
	 * 处理追加轮次
	 */
	private void handleAppendTurn() {
		while (!appendTurns.isEmpty()) {
			if (this.play.teamVS.isOver()) {
				// 已经分出胜负
				break;
			}
			appendTurns.sort(Comparator.comparing((AppendTurn turn) -> -turn.actor.attr.total().speedTotal()));
			AppendTurn append = appendTurns.removeFirst();
			if (!append.inValid(this)) {
				append.beforeAppendTurn(this);
				TurnCtx turnCtx = new TurnCtx(this, append.actor, append.skill,true);
				//开始行动
				turnCtx.start();
			}else {
				append.onAppendTurnFail(this);
			}
		}
	}

	public PlayCtx getPlay() {
		return play;
	}

	public AllPlayCfg getCfg(){
		return play.getCfg();
	}

	public TeamVS getTeamVs(){
		return play.teamVS;
	}

	//是奇数回合
	public boolean isOddRound() {
		return index % 2 == 1;
	}

	//是偶数回合
	public boolean isEvenRound() {
		return !isOddRound();
	}
}
